package name.fuhan.swiperefreshvpagerindicator.test;

import java.util.ArrayList;

import name.fuhan.swiperefreshvpagerindicator.R;
import name.fuhan.swiperefreshvpagerindicator.R.layout;
import name.fuhan.swiperefreshvpagerindicator.view.VPagerIndicator;
import name.fuhan.swiperefreshvpagerindicator.view.VPagerIndicator.MyListViewAdapter;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private VPagerIndicator vPager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
vPager = (VPagerIndicator) findViewById(R.id.vPager);
vPager.setAdapters(new MyAdapter("类型0", 0),new MyAdapter("类型1", 1),new MyAdapter("类型2", 2));
	}
	
	class MyAdapter extends MyListViewAdapter<Bean> implements OnClickListener{
		private String title;
		private int type;
		ArrayList<Bean> data = new ArrayList<Bean>();
		public MyAdapter(String title,int type) {
			super(MainActivity.this);
			this.title = title;
			this.type = type;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView==null) {
				convertView = View.inflate(MainActivity.this, R.layout.item_test1, null);
				convertView.findViewById(R.id.button1).setOnClickListener(this);
			}
			TextView textView = (TextView) convertView.findViewById(R.id.textView1);
			Button btnView = (Button) convertView.findViewById(R.id.button1);
			Bean bean = getItem(position);
			textView.setText(bean.text);
			btnView.setText(bean.btn);
			btnView.setTag(bean);
			switch (type) {
			case 0:
				btnView.setBackgroundColor(Color.CYAN);
				break;
			case 1:
				btnView.setBackgroundColor(Color.YELLOW);
				break;
			case 2:
				btnView.setBackgroundColor(Color.GREEN);
				break;
			default:
				break;
			}
			return convertView;
		}
		@Override
		public String getPageTitle() {
			return title;
		}
		@Override
		public ArrayList<Bean> getData() {
			return data;
		}
		@Override
		public void onRefresh(final SwipeRefreshLayout sw) {
			new Thread(){
				public void run() {
					SystemClock.sleep(1000);
					ArrayList<Bean> data = new ArrayList<Bean>();
					int s = (int) (Math.random()*500);
					for (int i = s; i < s+15; i++) {
						data.add(new Bean("文字"+i, "按钮"+i));
					}
					MyAdapter.this.data = data;
					notifyDataSetChanged();
					runOnUiThread(new Runnable() {
						public void run() {
							sw.setRefreshing(false);
						}
					});
				}
			}.start();
		}
		@Override
		public void onClick(View v) {
			Bean bean = (Bean) v.getTag();
			Toast.makeText(MainActivity.this, bean.text+","+bean.btn, 0).show();
		}
		
	}
}
